public class HasPathSum {

    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) return false;
        return process(root, 0, targetSum);
    }

    public boolean process(TreeNode node, int pre, int target) {
        if (node.left == null && node.right == null) {
            return pre + node.val == target;
        }
        boolean res1 = false;
        boolean res2 = false;
        if (node.left != null) {
            res1 = process(node.left, pre + node.val, target);
        }
        if (node.right != null) {
            res2 = process(node.right, pre + node.val, target);
        }
        return res1 || res2;
    }
}
